﻿----------
-- 战斗提示插件 https://warcraft.wiki.gg/wiki/Warcraft_Wiki:Interface_customization
----------
local alertFrame = CreateFrame("Frame")

-- 创建实例
local app = LibStub("AceAddon-3.0"):NewAddon("combat", "AceEvent-3.0")

-- 首次加载插件时
function app:OnInitialize()
    -- print("战斗插件加载!")
end

-- 启用插件时
function app:OnEnable()
    -- self:Print("欢迎使用! ")
    -- 注册事件
    -- 玩家进入战斗状态
    self:RegisterEvent("PLAYER_REGEN_DISABLED")
    -- 玩家离开战斗状态
    self:RegisterEvent("PLAYER_REGEN_ENABLED")
	alertFrame = initUI()
end

-- 玩家进入战斗状态
function app:PLAYER_REGEN_DISABLED()

    -- 判断是否开启
    if _G["AddonUiDB"].profile.combatOn ~= true then
        return
    end
	-- print('进入战斗')
	alertFrame:Show()
	alertFrame.text:SetText('进入战斗')
	alertFrame:SetPoint("TOP", 0, _G["AddonUiDB"].profile.combatY)

	local alpha = 0
	local timer =C_Timer.NewTicker(0.1, function()
		alpha = alpha + 0.2
		if alpha >= 1 then
			alertFrame:SetAlpha(1)
			timer:Cancel()
		else
			alertFrame:SetAlpha(alpha)
		end
	end, 5)
end
-- 玩家离开战斗
function app:PLAYER_REGEN_ENABLED()
    -- print('离开战斗')
    -- 判断是否开启
    if _G["AddonUiDB"].profile.combatOn ~= true then
        return
    end
	alertFrame.text:SetText('离开战斗')
	alertFrame:SetPoint("TOP", 0, _G["AddonUiDB"].profile.combatY)
	
	local alpha = 1
	local timer =C_Timer.NewTicker(0.1, function()
		alpha = alpha - 0.1
		if alpha <= 0 then
			alertFrame:SetAlpha(0)
			alertFrame:Hide()
			timer:Cancel()
		else
			alertFrame:SetAlpha(alpha)
		end
	end, 10)

end

-- 创建ui
function initUI()
	alertFrame:SetSize(400, 55)
	alertFrame:SetPoint("TOP", 0, -160)
	alertFrame:SetScale(0.65)
	alertFrame:Hide()
	alertFrame:SetAlpha(0)

	alertFrame.Bg = alertFrame:CreateTexture(nil, "BACKGROUND")
	alertFrame.Bg:SetTexture("Interface\\LevelUp\\MinorTalents")
	alertFrame.Bg:SetPoint("TOP")
	alertFrame.Bg:SetSize(400, 57)
	alertFrame.Bg:SetTexCoord(0, 400/512, 341/512, 407/512)
	alertFrame.Bg:SetVertexColor(1, 1, 1, 0.4)
	alertFrame.text = alertFrame:CreateFontString(nil, "ARTWORK", "GameFont_Gigantic")
	-- alertFrame.text:SetFont("Fonts\\ARIALN.TTF", 30)
	alertFrame.text:SetPoint("CENTER")
	return alertFrame
end


